<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="用户名称" prop="userId">
    <!--  <el-input v-model="dataForm.userId" placeholder="用户"></el-input>-->
     <el-select v-model="dataForm.userId" placeholder="请选择用户名称">
             <el-option
               v-for="item in userList"
               :key="item.id"
               :label="item.userUsername"
               :value="item.id">
             </el-option>
           </el-select>
    </el-form-item>





    <el-form-item label="水表编号" prop="waterId">
      <el-input v-model="dataForm.waterId" placeholder="水表编号"></el-input>
    </el-form-item>
    <el-form-item label="水表跑数" prop="waterCount">
      <el-input v-model="dataForm.waterCount" placeholder="水表跑数"></el-input>
    </el-form-item>
    <el-form-item label="上月水表跑数" prop="waterBeforecount">
      <el-input v-model="dataForm.waterBeforecount" placeholder="上月水表跑数"></el-input>
    </el-form-item>
    <el-form-item label="抄表时间" prop="waterTime">
    <!-- <el-input v-model="dataForm.waterTime" placeholder="抄表时间"></el-input> -->


      <el-date-picker
              v-model="dataForm.waterTime"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择抄表时间">
            </el-date-picker>
    </el-form-item>
<!-- -->
  <el-form-item label="水费价格" prop="waterPriceid">
  <!--      <el-input v-model="dataForm.waterPriceid" placeholder="水费价格编号"></el-input> -->


       <el-select v-model="dataForm.waterPriceid" placeholder="请选择水价">
                <el-option
                  v-for="item in waterList"
                  :key="item.id"
                  :label="item.waterprice"
                  :value="item.id">
                </el-option>
              </el-select>
    </el-form-item>

<!--
<el-form-item label="水费缴费状态" prop="waterStatus">
      <el-input v-model="dataForm.waterStatus" placeholder="水费缴费状态（待缴费，已缴费）"></el-input>
    </el-form-item>

      <el-form-item label="缴纳费用" prop="waterMoney">
           <el-input v-model="dataForm.waterMoney" placeholder="缴纳费用"></el-input>
         </el-form-item>
         <el-form-item label="支付时间" prop="waterPayTime">
           <el-input v-model="dataForm.waterPayTime" placeholder="支付时间"></el-input>
         </el-form-item>-->



    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          waterId: '',
          waterCount: '',
          waterBeforecount: '',
          waterTime: '',
          waterStatus: '',
          waterPriceid: '',
          waterMoney: '',
          waterPayTime: ''
        },
        userList: [],
        waterList: [],
        dataRule: {
          userId: [
            { required: true, message: '用户编号不能为空', trigger: 'blur' }
          ],
          waterId: [
            { required: true, message: '水表编号不能为空', trigger: 'blur' }
          ],
          waterCount: [
            { required: true, message: '水表跑数不能为空', trigger: 'blur' }
          ],
          waterBeforecount: [
            { required: true, message: '上月水表跑数不能为空', trigger: 'blur' }
          ],
          waterTime: [
            { required: true, message: '抄表时间不能为空', trigger: 'blur' }
          ],
          waterStatus: [
            { required: true, message: '水费缴费状态（待缴费，已缴费）不能为空', trigger: 'blur' }
          ],
          waterPriceid: [
            { required: true, message: '水费价格编号不能为空', trigger: 'blur' }
          ],
          waterMoney: [
            { required: true, message: '缴纳费用不能为空', trigger: 'blur' }
          ],
          waterPayTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getAllList()
      this.getAllListwater()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/use/appwater/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.appWater.userId
                this.dataForm.waterId = data.appWater.waterId
                this.dataForm.waterCount = data.appWater.waterCount
                this.dataForm.waterBeforecount = data.appWater.waterBeforecount
                this.dataForm.waterTime = data.appWater.waterTime
                this.dataForm.waterStatus = data.appWater.waterStatus
                this.dataForm.waterPriceid = data.appWater.waterPriceid
                this.dataForm.waterMoney = data.appWater.waterMoney
                this.dataForm.waterPayTime = data.appWater.waterPayTime
              }
            })
          }
        })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/use/appwater/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'waterId': this.dataForm.waterId,
                'waterCount': this.dataForm.waterCount,
                'waterBeforecount': this.dataForm.waterBeforecount,
                'waterTime': this.dataForm.waterTime,
                'waterStatus': this.dataForm.waterStatus,
                'waterPriceid': this.dataForm.waterPriceid,
                'waterMoney': this.dataForm.waterMoney,
                'waterPayTime': this.dataForm.waterPayTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
       // 获取用户下拉框
      getAllList () {
        this.$http({
          url: this.$http.adornUrl('/use/appuser/listAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.userList = data.list
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 查水费电费价格
      getAllListwater () {
        this.$http({
          url: this.$http.adornUrl('/use/appprice/listAll'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.waterList = data.list
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      }
    }
  }
</script>
